package top.hcode.hoj.service.admin.course;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.nutz.lang.Lang;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.dao.LinkLoader;
import top.hcode.hoj.dao.course.LessonDao;
import top.hcode.hoj.dao.course.LessonWorkQuestionDao;
import top.hcode.hoj.dao.course.QuestionDao;
import top.hcode.hoj.dao.course.QuestionOptionDao;
import top.hcode.hoj.pojo.dto.QueryQuestionDTO;
import top.hcode.hoj.pojo.dto.SaveQuestionDTO;
import top.hcode.hoj.pojo.entity.course.Lesson;
import top.hcode.hoj.pojo.entity.course.Question;
import top.hcode.hoj.pojo.entity.course.QuestionOption;
import top.hcode.hoj.pojo.vo.QuestionVO;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author orangej
 * @since 2024/12/18
 */
@Component
public class AdminQuestionService {
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private QuestionOptionDao optionDao;
    @Autowired
    private LessonDao lessonDao;
    @Autowired
    private LessonWorkQuestionDao workQuestionDao;
    @Autowired
    private LinkLoader linkLoader;
    @Autowired
    private LazyDao lazyDao;

    public IPage<QuestionVO> getQuestionList(QueryQuestionDTO dto) {
        return questionDao.queryQuestionVOPage(dto);
    }

    public QuestionVO getQuestionDetail(Long id) {
        QuestionVO questionVO = questionDao.fetchQuestionVO(id);
        List<QuestionOption> optionList = optionDao.queryOptionsByQuestionId(id);
        questionVO.setOptions(optionList);
        return questionVO;
    }

    /**
     * 保存习题
     *
     * @param question 习题
     * @return 习题
     */
    @Transactional
    public Question saveQuestion(SaveQuestionDTO question) {
        if (question.getHasChildren() == null) {
            question.setHasChildren(false);
        }

        questionDao.saveOrUpdate(question); // 保存习题

        List<QuestionOption> optionList = question.getOptions();
        if (Lang.isEmpty(optionList)) {
            return question;
        }

        // 选项不为空，需要处理选项相关的逻辑
        Long questionId = question.getId();
        List<Long> optionIdList = optionList.stream().map(QuestionOption::getId)
                .filter(Objects::nonNull)
                .distinct().collect(Collectors.toList());

        // 删除编辑内容中不存在的选项
        optionDao.queryOptionsByQuestionId(questionId).stream()
                .filter(o -> !optionIdList.contains(o.getId()))
                .forEach(o -> optionDao.removeById(o.getId()));

        // 保存选项
        int order = 1;
        StringBuilder answer = new StringBuilder();
        for (QuestionOption option : optionList) {
            option.setQuestionId(questionId);
            option.setSortOrder(order++);
            optionDao.saveOrUpdate(option);

            if (BooleanUtil.isTrue(option.getIsAnswer())) {
                answer.append(option.getOptionNum());
            }
        }

        // 保存答案
        question.setAnswer(answer.toString());
        questionDao.saveOrUpdate(question);

        return question;
    }

    @Transactional
    public boolean deleteQuestion(Long id) {
        optionDao.remove(new QueryWrapper<QuestionOption>().eq("question_id", id));
        return questionDao.removeById(id);
    }

}
